
package com.sanadc.shohdi;



import java.awt.*;
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.event.*;

public class FontPropertiesPanel extends JPanel
{
	protected JList nameList;
	protected JComboBox sizeBox;
	protected JCheckBox boldBox;
	protected JCheckBox italicBox;
	
	
	protected SampleTextFrame frame;
	
	
	public final static int[] fontSizes = {10,12,14,18,24,32,48,64};
	
	public FontPropertiesPanel (SampleTextFrame stf)
	{
		super();
		frame  = stf;
		createComponents();
		buildLayout();
		
	}
	
	
	protected void buildLayout()
	{
		JLabel label;
		GridBagConstraints gbc = new GridBagConstraints();
		GridBagLayout gbl = new  GridBagLayout();
		setLayout (gbl);
		
		gbc.anchor = GridBagConstraints.WEST;
		
		gbc.insets = new Insets(5, 10, 5, 10);
		
		gbc.gridx = 0;
		
		label = new JLabel ("Name:",Label.LEFT);
		gbl.setConstraints(label,gbc);
		add(label);
		
		label = new JLabel ("Size:",JLabel.LEFT);
		gbl.setConstraints(label,gbc);
		add (label);
		gbl.setConstraints(boldBox,gbc);
		add(boldBox);
		
		gbc.gridx ++;
		
		nameList.setVisibleRowCount(3);
		JScrollPane jsp = new JScrollPane(nameList);
		gbl.setConstraints(jsp,gbc);
		add (jsp);
		gbl.setConstraints(sizeBox,gbc);
		add (sizeBox);
		gbl.setConstraints(italicBox,gbc);
		add (italicBox);
		
		
	}
	
	
	
	protected void createComponents()
	{
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		String [] names = ge.getAvailableFontFamilyNames();
		nameList = new JList(names);
		nameList.setSelectedIndex(0);
		nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		nameList.addListSelectionListener(new ListSelectionListener(){
			public void valueChanged(ListSelectionEvent event)
			{
				handleFontPropertyChange();
			}
		});
		
		
		Integer sizes[] = new Integer[fontSizes.length];
		for(int i=0;i<sizes.length;i++)
		{
			sizes[i] = new Integer(fontSizes[i]);
			
		}
		
		sizeBox = new JComboBox(sizes);
		sizeBox.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent event) {
				// TODO Auto-generated method stub
				handleFontPropertyChange();
				
			}
		});
		
		
		boldBox = new JCheckBox("Bold");
		boldBox.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent event) {
				// TODO Auto-generated method stub
				handleFontPropertyChange();
			}
		});
		
		
		italicBox = new JCheckBox("Italic");
		italicBox.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent event) {
				// TODO Auto-generated method stub
				handleFontPropertyChange();
			}
		});
		
		
		
	}
	
	protected void handleFontPropertyChange()
	{
		frame.refreshDisplayFont();
	}
	
	public String getSelectedFontName()
	{
		return (String)(nameList.getSelectedValue());
		
	}
	
	
	public int getSelectedFontSize()
	{
		return ((Integer)(sizeBox.getSelectedItem())).intValue();
		
	}
	
	
	public boolean isBoldSelected()
	{
		return boldBox.isSelected();
	}
	
	public boolean isItalicSelected()
	{
		return italicBox.isSelected();
	}
	
	
	
}





	
	
